package com.jeeinc.oa.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationStrategy;
import org.springframework.web.accept.FixedContentNegotiationStrategy;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

@EnableWebMvc
@ComponentScan("com.jeeinc.oa.controller")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

	private SessionFactory sessionFactory;

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/**");
		registry.setOrder(Ordered.LOWEST_PRECEDENCE);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
		registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		OpenSessionInViewInterceptor open = new OpenSessionInViewInterceptor();
		open.setSessionFactory(sessionFactory);
		registry.addWebRequestInterceptor(open);
	}

	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		resolver.setMaxUploadSize(1000000);
		return resolver;
	}

	@Bean
	public ViewResolver contentNegotiatingViewResolver() {
		ContentNegotiatingViewResolver contentView = new ContentNegotiatingViewResolver();
		Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>();
		mediaTypes.put("json", MediaType.APPLICATION_JSON);
		PathExtensionContentNegotiationStrategy pathExtension = new PathExtensionContentNegotiationStrategy(mediaTypes);
		pathExtension.setUseJaf(false);
		HeaderContentNegotiationStrategy header = new HeaderContentNegotiationStrategy();
		FixedContentNegotiationStrategy fixed = new FixedContentNegotiationStrategy(MediaType.ALL);
		contentView.setContentNegotiationManager(new ContentNegotiationManager(new ContentNegotiationStrategy[] { pathExtension, header, fixed }));

		contentView.setViewResolvers(new ArrayList<ViewResolver>() {
			private static final long serialVersionUID = 1L;
			{
				InternalResourceViewResolver view = new InternalResourceViewResolver();
				view.setSuffix(".jsp");
				view.setPrefix("/WEB-INF/views/");
				add(view);
			}
		});
		contentView.setDefaultViews(new ArrayList<View>() {
			private static final long serialVersionUID = 1L;
			{
				add(new MappingJackson2JsonView());
			}
		});
		return contentView;
	}

}
